<template>
	<view>
		<view class="search-box">
            <my-search @click="gotoSearch"></my-search>
        </view>
		<view class="scroll-view-container">
			<!-- 左侧滑动区域 -->
			<scroll-view class="left-scroll-view" scroll-y style="height: 100vh;">
				<view class="left-scroll-view-item" :class="{active: i === activeIndex ? 'active': ''}"
					v-for="(item,i) in cateList" :key="i" @click="onclickToggle(i)">{{item.cat_name}}</view>
			</scroll-view>

			<!-- 右侧滑动区域 -->
			<scroll-view class="right-scroll-view" scroll-y style="height: 100vh;" :scroll-top="scrollTop">
				<view class="right-scroll-view-item active">
					<view class="cate-l2" v-for="(item2,i2) in cateLevel2" :key="i2">
						<!-- 二级分类标题 -->
						<view class="cate-l2-title">/ {{item2.cat_name}} /</view>
						<!-- 当前二级分类下的三级分类区域 -->
						<view class="cate-l3-list">
							<!-- 三级分类的每一项 -->
							<view class="cate-l3-item" v-for="(item3,i3) in item2.children" :key="i3" @click="gotoGoodsList(item3)">
								<!-- 图片 -->
								<image :src="item3.cat_icon" class="cate-l3-image"></image>
								<!-- 文本 -->
								<text class="cate-l3-text">{{item3.cat_name}}</text>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
    import tabBarBadge from "@/mixins/tabBar-badge.js";
	export default {
        mixins:[tabBarBadge],
		data() {
			return {
				scrollTop: 0,
				activeIndex: 0,
				cateList: [],
				// 二级分类
				cateLevel2: []
			};
		},
		onLoad() {
			// const systemInfo = uni.getSystemInfoSync()
			this.getCateList()
		},
        onPullDownRefresh() {
            this.getCateList()
            uni.stopPullDownRefresh()
        },
		methods: {
			async getCateList() {
				const res = await uni.$http.get('/api/public/v1/categories')
				if (res.data.meta.status !== 200) return uni.$showMsg()
				this.cateList = res.data.message
				this.cateLevel2 = res.data.message[0].children
				// console.log(res.data.message[0].children);
				// console.log(res.data.message);
			},
			onclickToggle(index) {
				this.activeIndex = index
				this.cateLevel2 = this.cateList[index].children
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			gotoGoodsList(item) {
				uni.navigateTo({
					url:`/subpackage/goods_list/goods_list?cid=${item.cat_id}`
				})
			},
			gotoSearch() {
				uni.navigateTo({
					url:'/subpackage/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	$base-font-size: 12px;

    .search-box {
        position: sticky;
        top: 0;
        z-index: 999;
    }

	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 240rpx;

			.left-scroll-view-item {
				position: relative;
				background-color: #f7f7f7;
				text-align: center;
				line-height: 50px;
				font-size: 14px;

				&.active {
					background-color: #ffffff;

					&:before {
						content: '';
						display: inline-block;
						width: 3px;
						height: 30px;
						background-color: #ff93a7;
						position: absolute;
						top: 50%;
						left: 0;
						transform: translateY(-50%);

					}
				}
			}
		}

		.right-scroll-view {

			.cate-l2-title {
				font-size: $base-font-size;
				font-weight: bold;
				text-align: center;
				padding: 15px 0;
			}

			.cate-l3-list {
				display: flex;
				flex-wrap: wrap;

				.cate-l3-item {
					width: 33.33%;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					margin-bottom: 20rpx;

					.cate-l3-image {
						width: 60px;
						height: 60px;
					}

					.cate-l3-text {
						font-size: $base-font-size;
					}
				}
			}
		}
	}
</style>